<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class UsersSeeder extends Seeder
{
    // php artisan db:seed --class=UsersSeeder

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 清空数据表 后在添加
        $time = time();
        $toDb = DB::table('user');
        $toDb->truncate();

        // 查询原来的数据
        $users = DB::select('select * from wp_users');
        $users = json_decode(json_encode($users), true);
        $all   = [];
        foreach($users as $_user){
            $all[] = $this->makeRow($_user, $time);
        }

        $toDb->insert($all);
    }


    // 生成数据
    protected function makeRow($_user, $time)
    {

        $_row = [
            'id'             => $_user['ID'],
            'loginname'      => $_user['user_login'],
            'password'       => $_user['user_pass'],
            'salt'           => '',
            'head_img'       => '',
            'nickname'       => $_user['user_nicename'],
            'showname'       => $_user['display_name'],
            'email'          => $_user['user_email'],
            'activation_key' => $_user['user_activation_key'],
            'status'         => 1,
            'ctime'          => strtotime($_user['user_registered']),
            'mtime'          => $time,
        ];

        return $_row;
    }

}
